<template>
  <div class="detail">
    <detailNavBar class="detail-navbar" ref="detailNavbar" @titleClick="titleClick"></detailNavBar>
    <scroll class="content" ref="scroll" :probeType="3" @scroll="detailScroll">
      <detail-swiper :topImages="topImages" @swiperImageLoad="imageLoad"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detailInfo="detailInfo" :DetailImageLoad="imageLoad"></detail-goods-info>
      <detail-param-info ref="param" :paramInfo="paramInfo"></detail-param-info>
      <detail-comment-info ref="comment" :commentInfo="commentInfo"></detail-comment-info>
      <goods-list ref="recommends" :goods="recommends"></goods-list>
    </scroll>
    <DetailButtomBar @addToCart="addToCart"></DetailButtomBar>
    <back-top @click.native="backClick" v-if="isShowBackTop"></back-top>
  </div>
</template>

<script>
import {getDetail,getRecommend,
        Goods,Shop,GoodsParam} from 'network/detail' //network网络请求
import {debounce} from 'common/util'
import detailNavBar from './childComps/DetailNavBar'
import detailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import DetailButtomBar from './childComps/DetailButtomBar'

import GoodsList from 'components/content/goods/GoodsList'

import Scroll from 'components/common/scroll/Scroll'

import {itemListenerMixin,backTopMixin} from 'common/mixin'

import { mapActions } from 'vuex'

export default {
  name:'Detail',
  components: {
    detailNavBar,
    detailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailButtomBar
  },
  mixins:[itemListenerMixin,backTopMixin],
  data() {
      return {
          iid:null,
          topImages:null, //顶部轮播数据
          goods:{},
          shop:{},   //
          detailInfo:{},
          paramInfo:{},
          commentInfo:{},
          recommends:[],
          themeTopYs:[],
          getThemeTopYsData:null,
          currentIndex:0   //顶部标题索引
      }
  },
  destroyed() {
    this.$bus.$off('itemImageLoad',this.itemImageListener)
  },
  mounted() {
    // this.themeTopYs.push(0)
    // this.themeTopYs.push(this.$refs.param.$el.offsetTop)
  },
  created() {
    //1.保存商品iid
    //this.iid = this.$route.params.iid

    this.iid = this.$route.query.iid

    //2.根据id请求数据
    getDetail(this.iid)
    .then(res=>{
        //拿到详细的全部数据
        const data = res.result
        console.log(data)
        //1.轮播图数据
        this.topImages = data.itemInfo.topImages

        //2.商品信息
        this.goods = new Goods(data.itemInfo,data.columns,data.shopInfo.services)

        //3.店铺信息
        this.shop = new Shop(data.shopInfo)

        //4.商品详情数据
        this.detailInfo = data.detailInfo

        //5.商品参数
        this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)

        //6.评论信息
        if(data.rate.cRate!=0){
           this.commentInfo = data.rate.list[0]  //拿一条进行展示
        }

        //  做法一:拿完数据直接获取  -- 不对 
        //  因为渲染还需要点时间或压根还没渲染,导致offsetTop不准
        // this.themeTopYs = []
        // this.themeTopYs.push(0)
        // this.themeTopYs.push(this.$refs.param.$el.offsetTop)
        // this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        // this.themeTopYs.push(this.$refs.recommends.$el.offsetTop)
        

        //  做法二:
        // this.$nextTick(()=>{
        //   //根据最新的数据，对应的dom已经是被渲染出来
        //   //但是图片还是没有完全被加载出来（目前获取的offsetTop不包含图片）
        //   this.themeTopYs = []
        //   this.themeTopYs.push(0)
        //   this.themeTopYs.push(this.$refs.param.$el.offsetTop)
        //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        //   this.themeTopYs.push(this.$refs.recommends.$el.offsetTop)
        // })
        

    })

    // 7.商品推荐数据展示
    getRecommend().then(value=>{
      this.recommends = value.data.list
    })

    this.getThemeTopYsData = debounce(()=>{
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.param.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommends.$el.offsetTop)
      this.themeTopYs.push(Number.MAX_VALUE)
    },1000)
  },
  methods: {
    ...mapActions(['addCart']),
    addToCart(){  //添加到购物车
      //1.获取购物车需要展示的信息
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid
      product.checked = true

      //2.将商品添加到购物车
      //this.$store.commit('addCart',product)
      // this.$store.dispatch('addCart',product)
      // .then(value=>{
      //   console.log(value)
      // })
      this.addCart(product)
      .then(value=>{
        this.$toast.show(value)
      })

    },
    imageLoad(){
       // this.$refs.scroll.scroll.refresh()
      this.scrollRefresh()

      this.getThemeTopYsData()
    },
    titleClick(index){
      this.$refs.scroll.scroll.scrollTo(0,-this.themeTopYs[index],100)
    },
    detailScroll(postion){
      //1.获取Y值
      const postionY = -postion.y

      //2.postionY和主题的值对比
      let length = this.themeTopYs.length
      for(let i=0;i<length-1;i++){
        if(this.currentIndex!==i && (postionY >= this.themeTopYs[i] && postionY < this.themeTopYs[i+1])){
          this.currentIndex = i
          this.$refs.detailNavbar.titleCurrentIndex = i

        }
      }

      //3.监听回到顶部组件显示
      this.isShowBackTop = (-postion.y) > 1000

    }
  },
}
</script>

<style scope>
.detail{
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.detail-navbar{
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.content{
  height: calc(100% - 44px);
}
</style>